<?php

namespace wangk\openapi\openapi\api;

use wangk\openapi\openapi\config\Config;
use wangk\openapi\openapi\oAuth\RequestClient;

class BaseApi
{
    private $token;
    private $config;

    public function __construct($token, Config $config)
    {
        $this->token = $token;
        $this->config = $config;
    }


    protected function call($action, $params, $requestType = 'post')
    {
        $params['request_id'] = $this->config->getAppKey();
        $params['secret_key'] = $this->config->getAppSecret();
        $requestUrl = $this->config->requestUrl() . $action;
        $params['timestamp'] = time();
        $params['sign'] = RequestClient::signature($params);
        unset($params['secret_key'], $params['request_id']);
        $headers = [
            'Authorization: Bearer ' . $this->token
        ];
        return RequestClient::request($requestUrl, $params, $headers, $requestType);
    }

}